const bodyPerser = require('body-parser')
const Mock = require('mockjs');
const fs = require('fs')
const users = require('./src/mock/user.json')
console.log(users);
const data = Mock.mock({
    'goodsList|15-30': [{
        img: '@image(100x100,@color)',
        title: '@ctitle(2,8)',
        id: '@id',
        text: '@cword(10,30)',
        'price|5-100': 0,
        'num|1000-100000': 0
    }],
    'shopCarList': [{
        img: '@image(100x100,@color)',
        title: '@ctitle(2,8)',
        id: '@id',
        text: '@cword(10,30)',
        'price|5-100': 0,
        'num|1-5': 0
    }]
})
module.exports = {
    devServer: {
        before(app) {
            app.get('/goodsList', (req, res) => {
                res.send(data.goodsList)
            })
            app.get('/goodsDetail', (req, res) => {
                const { id } = req.query
                const newArr = data.goodsList.filter(item => {
                    return item.id == id
                })
                res.send(newArr[0])
            })
            app.use(bodyPerser.json())
                // 登录接口
            app.post('/toLogin', (req, res) => {

                const { username, password } = req.body
                const newArr = users.filter(item => {
                        return item.username == username
                    })
                    // 判断数组长度
                if (newArr.length) {
                    // 判断密码是否一致
                    if (newArr[0].password == password) {
                        // 如果一致，说明登录成功
                        res.send({
                            code: 200,
                            msg: '登录成功'
                        })
                    } else {
                        // 否则， 登录失败
                        res.send({
                            code: 401,
                            msg: '登录失败'
                        })
                    }
                } else {
                    // 数组没有长度，说明没有注册
                    res.send({
                        code: 205,
                        msg: '您还未注册，请先注册'
                    })
                }
            })

            // 注册接口
            app.post('/toReg', (req, res) => {
                const { username, password } = req.body
                    // 判断是否已注册
                const newArr = users.filter(item => {
                    return item.username == username
                })
                if (newArr.length) {
                    res.send({
                        code: 402,
                        msg: '该用户已注册，直接登录'
                    })
                } else {
                    users.push({
                        username,
                        password
                    })
                    fs.writeFileSync('./src/mock/user.json', JSON.stringify(users))
                    res.send({
                        code: 201,
                        msg: '注册成功'
                    })
                }
            })
            app.get('/shopCarList', (req, res) => {
                res.send(data.shopCarList)
            })
        }
    }
}